package com.salesmonitoring.accounts.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import org.jdesktop.swingx.JXDatePicker;

import com.salesmonitoring.accounts.controller.AdminController;
import com.salesmonitoring.accounts.controller.MarketingAssistantController;
import com.salesmonitoring.accounts.model.User;
import com.salesmonitoring.model.Motor;
import com.salesmonitoring.model.RepairShop;
import java.awt.Font;

public class AddReportUI extends JFrame implements ActionListener {

	private JPanel addReportPanel;
	private JLabel lblAccountOfficer, lblOldCustName;
	private JComboBox accountOfficerComboBox;

	private ArrayList<User> users;
	private JTextField oldCustNameTextField;
	private JLabel lblNewCustName;
	private JTextField newCustNameTextField;
	private JLabel lblDateRequested;
	private JXDatePicker dateRequestedPicker;
	private JLabel lblDateOfRepo;
	private JXDatePicker dateRepoPicker;
	private JLabel lblDateSold;
	private JXDatePicker dateSoldPicker;
	private JLabel lblRepairShop;
	private JComboBox repairShopComboBox;
	private JLabel lblRepairShopOwner;
	private JTextField repairShopOwnerTextField;
	private JLabel lblRepairShopAddress;
	private JTextField repairShopAddressTextField;
	private JLabel lblRepairShopWarehouse;
	private JTextField repairShopWarehouseTextField;
	private JLabel lblModel;
	private JTextField motorModelTextField;
	private JLabel lblEngine;
	private JTextField motorEngineTextField;
	private JLabel lblChassis;
	private JTextField motorChassisTextField;
	private JLabel lblPlateNumber;
	private JTextField motorPlateNumberTextField;
	private JLabel lblClass;
	private JTextField motorClassTextField;
	private JLabel lblWarehouse;
	private JComboBox motorWarehouseComboBox;
	private JButton addButton;
	private JComboBox motorEngineComboBox, accountNumcomboBox;
	
	private String[] names, shops, warehouses, accountNumbers, engineNumbers;
	private boolean aoFlag = false, newCustomerFlag = false, rsFlag = false,
			engineFlag = false, warehouseFlag  = false, accountNumberFlag = false;
	private RepairShop shop;
	private ArrayList<Object> details;
	private JTextField textField;
	private JLabel lblAsmf;
	private JTextField textField_1;
	private JLabel lblAsmfNumber;
	private JTextField asmfNumberTextField;
	private JLabel lblAsmf_1;
	private JTextField textField_2;
	private JLabel lblAccountNumber;
	private JComboBox comboBox;

	public AddReportUI() {

		AdminController adminController = AdminController.getInstance();
		MarketingAssistantController marketingAssistantController = MarketingAssistantController.getInstance();
		
		this.users = adminController.getAllUsers();
		this.names = adminController.getAllAONames();
		this.shops = adminController.getAllRepairShops();
		this.warehouses = adminController.getAllWarehouses();
		this.accountNumbers = adminController.getAllAccountNumbers();
		this.engineNumbers = marketingAssistantController.getAllEngineNumbers();
		
		initComponents();
	}

	public void initComponents() {

		setTitle("Bank of Makati | Add Report");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 640, 400);
		addReportPanel = new JPanel();
		addReportPanel.setLayout(null);
		setContentPane(addReportPanel);

		// ---Account officer combo box
		lblAccountOfficer = new JLabel("Account Officer");
		lblAccountOfficer.setBounds(15, 15, 137, 16);
		addReportPanel.add(lblAccountOfficer);

		accountOfficerComboBox = new JComboBox(names);
		accountOfficerComboBox.setBounds(164, 11, 157, 27);
		addReportPanel.add(accountOfficerComboBox);

		accountNumcomboBox = new JComboBox(accountNumbers);
		accountNumcomboBox.setBounds(164, 39, 157, 27);
		accountNumcomboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				AdminController adminController = AdminController.getInstance();
				String number = accountNumcomboBox.getSelectedItem().toString();
				String oldCustomerName = adminController.getCustomerByAccountNumber(number);
				oldCustNameTextField.setText(oldCustomerName);
				
			}
		});
		
		addReportPanel.add(accountNumcomboBox);
		
		// ---Old cust name textfield
		lblOldCustName = new JLabel("Old Customer Name ");
		lblOldCustName.setBounds(15, 77, 157, 16);
		addReportPanel.add(lblOldCustName);

		oldCustNameTextField = new JTextField();
		oldCustNameTextField.setBounds(164, 71, 157, 28);
		oldCustNameTextField.setColumns(10);
		oldCustNameTextField.setEditable(false);
		addReportPanel.add(oldCustNameTextField);

		// ---New customer name textfield
		lblNewCustName = new JLabel("New Customer Name");
		lblNewCustName.setBounds(15, 105, 137, 16);
		addReportPanel.add(lblNewCustName);

		newCustNameTextField = new JTextField();
		newCustNameTextField.setColumns(10);
		newCustNameTextField.setBounds(164, 99, 157, 28);
		addReportPanel.add(newCustNameTextField);

		// ---Date requested picker
		lblDateRequested = new JLabel("Date Requested");
		lblDateRequested.setBounds(15, 141, 137, 16);
		addReportPanel.add(lblDateRequested);

		dateRequestedPicker = new JXDatePicker();
		dateRequestedPicker.setDate(Calendar.getInstance().getTime());
		dateRequestedPicker.setFormats(new SimpleDateFormat("dd.MM.yyyy"));
		dateRequestedPicker.setBounds(164, 135, 157, 28);
		addReportPanel.add(dateRequestedPicker);

		// ---Date of repo picker
		lblDateOfRepo = new JLabel("Date of Repo");
		lblDateOfRepo.setBounds(15, 177, 137, 16);
		addReportPanel.add(lblDateOfRepo);

		dateRepoPicker = new JXDatePicker();
		dateRepoPicker.setDate(Calendar.getInstance().getTime());
		dateRepoPicker.setFormats(new SimpleDateFormat("dd.MM.yyyy"));
		dateRepoPicker.setBounds(164, 169, 157, 28);
		addReportPanel.add(dateRepoPicker);

		// ---Date sold picker
		lblDateSold = new JLabel("Date Sold");
		lblDateSold.setBounds(15, 209, 137, 16);
		addReportPanel.add(lblDateSold);

		dateSoldPicker = new JXDatePicker();
		dateSoldPicker.setDate(Calendar.getInstance().getTime());
		dateSoldPicker.setFormats(new SimpleDateFormat("dd.MM.yyyy"));
		dateSoldPicker.setBounds(164, 203, 157, 28);
		addReportPanel.add(dateSoldPicker);

		// --------------Repair Shop Details------------------//

		// ---Repair shop text field
		lblRepairShop = new JLabel("Repair Shop");
		lblRepairShop.setBounds(15, 237, 137, 16);
		addReportPanel.add(lblRepairShop);

		repairShopComboBox = new JComboBox(shops);
		repairShopComboBox.setBounds(164, 233, 157, 27);
		repairShopComboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				AdminController adminController = AdminController.getInstance();
				shop = adminController.getShopBycode(repairShopComboBox.getSelectedItem().toString());

				repairShopOwnerTextField.setText(shop.getOwner());
				repairShopAddressTextField.setText(shop.getAddress());
				repairShopWarehouseTextField.setText(shop.getWarehouse());

			}
		});

		addReportPanel.add(repairShopComboBox);

		// ---Repair shop owner text field
		lblRepairShopOwner = new JLabel("Repair Shop Owner");
		lblRepairShopOwner.setBounds(15, 265, 137, 16);
		addReportPanel.add(lblRepairShopOwner);

		repairShopOwnerTextField = new JTextField();
		repairShopOwnerTextField.setColumns(10);
		repairShopOwnerTextField.setBounds(164, 259, 157, 28);
		repairShopOwnerTextField.setEditable(false);
		repairShopOwnerTextField.setText("Owner Name");
		addReportPanel.add(repairShopOwnerTextField);

		// ---Repair shop address textfield
		lblRepairShopAddress = new JLabel("Repair Shop Address");
		lblRepairShopAddress.setBounds(15, 293, 137, 16);
		addReportPanel.add(lblRepairShopAddress);

		repairShopAddressTextField = new JTextField();
		repairShopAddressTextField.setColumns(10);
		repairShopAddressTextField.setBounds(164, 287, 157, 28);
		repairShopAddressTextField.setEditable(false);
		repairShopAddressTextField.setText("Shop Address");
		addReportPanel.add(repairShopAddressTextField);

		// ---Repair shop warehouse combo box
		lblRepairShopWarehouse = new JLabel("Repair Shop Warehouse");
		lblRepairShopWarehouse.setBounds(15, 321, 157, 16);
		addReportPanel.add(lblRepairShopWarehouse);

		repairShopWarehouseTextField = new JTextField();
		repairShopWarehouseTextField.setBounds(164, 315, 157, 27);
		repairShopWarehouseTextField.setEditable(false);
		repairShopWarehouseTextField.setText("Warehouse Assigned");
		addReportPanel.add(repairShopWarehouseTextField);

		// --------------Motorcycle Details-------------------//

		// --- Motorcycle Engine Dropdown
		lblEngine = new JLabel("Engine Number");
		lblEngine.setBounds(350, 77, 137, 16);
		addReportPanel.add(lblEngine);

		motorEngineComboBox = new JComboBox(engineNumbers);
		motorEngineComboBox.setBounds(456, 73, 157, 27);
		motorEngineComboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				MarketingAssistantController marketingAssistantController = MarketingAssistantController.getInstance();
				String number = motorEngineComboBox.getSelectedItem().toString();
				
				Motor motor = marketingAssistantController.getMotorByEngineNumber(number);
				
				motorModelTextField.setText(motor.getModel());
				motorChassisTextField.setText(motor.getChassisNumber());
				motorPlateNumberTextField.setText(motor.getPlateNumber());
				motorClassTextField.setText(motor.getMotorClass());
				asmfNumberTextField.setText(motor.getAsmf());
				
			}
		});
		addReportPanel.add(motorEngineComboBox);

		// ---Motorcycle model textfield
		lblModel = new JLabel("Model");
		lblModel.setBounds(350, 105, 137, 16);
		addReportPanel.add(lblModel);
		
		motorModelTextField = new JTextField();
		motorModelTextField.setText("Motorcycle Model");
		motorModelTextField.setColumns(10);
		motorModelTextField.setBounds(456, 99, 157, 28);
		motorModelTextField.setEditable(false);
		addReportPanel.add(motorModelTextField);


		// --- Motorcycle chasis textfield

		lblChassis = new JLabel("Chassis");
		lblChassis.setBounds(350, 141, 137, 16);
		addReportPanel.add(lblChassis);

		motorChassisTextField = new JTextField();
		motorChassisTextField.setText("Motorcycle Chassis");
		motorChassisTextField.setColumns(10);
		motorChassisTextField.setBounds(456, 135, 157, 28);
		motorChassisTextField.setEditable(false);
		addReportPanel.add(motorChassisTextField);

		// ---Motorcycle plate number textfield

		lblPlateNumber = new JLabel("Plate Number");
		lblPlateNumber.setBounds(350, 177, 137, 16);
		addReportPanel.add(lblPlateNumber);

		motorPlateNumberTextField = new JTextField();
		motorPlateNumberTextField.setText("Plate Number");
		motorPlateNumberTextField.setColumns(10);
		motorPlateNumberTextField.setBounds(456, 171, 157, 28);
		motorPlateNumberTextField.setEditable(false);
		addReportPanel.add(motorPlateNumberTextField);

		// ---Motorcycle class textfield

		lblClass = new JLabel("Class");
		lblClass.setBounds(350, 209, 137, 16);
		addReportPanel.add(lblClass);

		motorClassTextField = new JTextField();
		motorClassTextField.setText("Class");
		motorClassTextField.setColumns(10);
		motorClassTextField.setBounds(456, 203, 157, 28);
		motorClassTextField.setEditable(false);
		addReportPanel.add(motorClassTextField);

		// ---Motorcycle warehouse Combo box

		lblWarehouse = new JLabel("Warehouse");
		lblWarehouse.setBounds(350, 237, 137, 16);
		addReportPanel.add(lblWarehouse);

		motorWarehouseComboBox = new JComboBox(warehouses);
		motorWarehouseComboBox.setBounds(456, 233, 157, 27);
		addReportPanel.add(motorWarehouseComboBox);

		// ---Motorcycle account number textfield
		lblAccountNumber = new JLabel("Account #");
		lblAccountNumber.setBounds(15, 43, 137, 16);
		addReportPanel.add(lblAccountNumber);
		
		// --Motorcycle ASMF number testfield
		lblAsmf_1 = new JLabel("ASMF #");
		lblAsmf_1.setBounds(350, 265, 137, 16);
		addReportPanel.add(lblAsmf_1);
		
		asmfNumberTextField = new JTextField();
		asmfNumberTextField.setBounds(456, 259, 157, 28);
		asmfNumberTextField.setColumns(10);
		asmfNumberTextField.setEditable(false);
		addReportPanel.add(asmfNumberTextField);
		
		// --Add Button
		addButton = new JButton("Add");
		addButton.setBounds(496, 316, 117, 29);
		addButton.addActionListener(this);
		addReportPanel.add(addButton);
		
		JLabel lblNewLabel = new JLabel("Motorcycle Information");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 14));
		lblNewLabel.setBounds(403, 42, 171, 16);
		addReportPanel.add(lblNewLabel);
		
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Border redBorder = BorderFactory.createEtchedBorder(Color.WHITE, Color.RED);
		Border defaultBorder = BorderFactory.createEtchedBorder(Color.WHITE, Color.GRAY);
		
		if(accountOfficerComboBox.getSelectedItem().equals("")) {
			accountOfficerComboBox.setBorder(redBorder);
			accountOfficerComboBox.requestFocus();
			aoFlag = false;
		} else {
			accountOfficerComboBox.setBorder(defaultBorder);
			aoFlag = true;
		}
		
		if(accountNumcomboBox.getSelectedItem().equals("")) {
			accountNumcomboBox.setBorder(redBorder);
			accountNumcomboBox.requestFocus();
			accountNumberFlag = false;
		} else {
			accountNumcomboBox.setBorder(defaultBorder);
			accountNumberFlag = true;
		}
		
		if(newCustNameTextField.getText().equals("")) {
			newCustNameTextField.setBorder(redBorder);
			newCustNameTextField.requestFocus();
			newCustomerFlag = false;
		} else {
			newCustNameTextField.setBorder(defaultBorder);
			newCustomerFlag = true;
		}
		
		if(repairShopComboBox.getSelectedItem().equals("")) {
			repairShopComboBox.setBorder(redBorder);
			repairShopComboBox.requestFocus();
			rsFlag = false;
		} else {
			repairShopComboBox.setBorder(defaultBorder);
			rsFlag = true;
		}
		
		if(motorEngineComboBox.getSelectedItem().equals("")) {
			motorEngineComboBox.setBorder(redBorder);
			motorEngineComboBox.requestFocus();
			engineFlag = false;
		} else {
			motorEngineComboBox.setBorder(defaultBorder);
			engineFlag = true;
		}
		
		
		if(motorWarehouseComboBox.getSelectedItem().equals("")) {
			motorWarehouseComboBox.setBorder(redBorder);
			motorWarehouseComboBox.requestFocus();
			warehouseFlag = false;
		} else {
			motorWarehouseComboBox.setBorder(defaultBorder);
			warehouseFlag = true;
		}

		
		if(aoFlag && accountNumberFlag && newCustomerFlag && rsFlag && engineFlag && warehouseFlag) {
			
			details = new ArrayList<Object>();
			MarketingAssistantController marketingAssistantController = MarketingAssistantController.getInstance();
			Date dateRequested = dateRequestedPicker.getDate();
			Date dateRepo = dateRepoPicker.getDate();
			Date dateSold = dateSoldPicker.getDate();
			
			Calendar calRequsted = Calendar.getInstance();
			Calendar calRepo = Calendar.getInstance();
			Calendar calSold = Calendar.getInstance();
			
			calRequsted.setTime(dateRequested);
			calRepo.setTime(dateRepo);
			calSold.setTime(dateSold);
			
			String requested = calRequsted.get(Calendar.YEAR) + "-" + (calRequsted.get(Calendar.MONTH)+1) + "-" + calRequsted.get(Calendar.DATE);
			String repo = calRepo.get(Calendar.YEAR) + "-" + (calRepo.get(Calendar.MONTH)+1) + "-" + calRepo.get(Calendar.DATE);
			String sold = calSold.get(Calendar.YEAR) + "-" + (calSold.get(Calendar.MONTH)+1) + "-" + calSold.get(Calendar.DATE);
			
			details.add(accountOfficerComboBox.getSelectedItem());
			details.add(oldCustNameTextField.getText());
			details.add(newCustNameTextField.getText());
			details.add(repairShopComboBox.getSelectedItem());
			details.add(motorModelTextField.getText());
			details.add(motorEngineComboBox.getSelectedItem());
			details.add(motorChassisTextField.getText());
			details.add(motorPlateNumberTextField.getText());
			details.add(motorClassTextField.getText());
			details.add(motorWarehouseComboBox.getSelectedItem());
			details.add(requested);
			details.add(repo);
			details.add(sold);
			details.add(accountNumcomboBox.getSelectedItem());
			details.add(asmfNumberTextField.getText());
			
			int status = marketingAssistantController.addReport(details);
			
			if(status == 0) {
				JOptionPane.showMessageDialog(null, "Error in adding reports.");
			} else {
				JOptionPane.showMessageDialog(null, "Report successfully addded.");
				this.dispose();
			}
			
		} else {
			JOptionPane.showMessageDialog(null, "Please fill out all fields.");
		}

	}
}
